package com.fcy.teamwork.business

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import com.fcy.teamwork.base.BaseRVAdapter
import com.fcy.teamwork.bean.User
import com.fcy.teamwork.databinding.ItemSelectMemberBinding
import com.fcy.teamwork.databinding.SelectSomeoneFromGroupBinding
import com.fcy.teamwork.pojo.Person

class SelectFromGroupListAdapter(
    private val isHideCheckBox: Boolean = false
) : BaseRVAdapter<Person, ItemSelectMemberBinding>() {
    private val selectedMembers = mutableListOf<Person>()
    override fun createBinding(
        inflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): ItemSelectMemberBinding {
        return ItemSelectMemberBinding.inflate(inflater, parent, false)
    }

    override fun bindView(holder: VH, position: Int, data: Person) {
        (holder.binding as? ItemSelectMemberBinding)?.apply {
            tvNikeName.text = data.nikeName
            if (isHideCheckBox) {
                cbSelect.visibility = View.GONE
            } else cbSelect.setOnClickListener {
                it as CheckBox
                if (it.isChecked) {
                    if (!selectedMembers.contains(data)) {
                        selectedMembers.add(data)
                    }
                } else {
                    if (selectedMembers.contains(data)) {
                        selectedMembers.remove(data)
                    }
                }
            }
        }
    }

    fun getSelectedMembers(): List<Person> = selectedMembers
}